
//获取系统UI框架对象，向上递归，找不到时返回null
//在系统首页中创建框架对象并命名为window.systemUiLayout3
//该方法在框架通过iframe加载的子页面中使用，获取系统框架对象
function getSystemUiLayout(win){
	win = win || window;
	if(win.systemUiLayout3){
		return win.systemUiLayout3;
	}
	if(win.parent && win.parent != win.self){
		return getSystemUiLayout(win.parent);
	}
	return null;
}

var sysui = null;
var UiUtil = {};

//打开表单界面
//opts:{url:'',title:'',refresh:''}
//params:[{name:'',value:''}]
UiUtil.openForm=function(opts, params){
	if(!sysui){
		sysui = getSystemUiLayout();
	}
	if(sysui){
		sysui.openForm(opts, params);
	}
};
//关闭g.center.formiframe
//参数succeeded：操作成功传入true，执行刷新等操作
UiUtil.closeForm=function(succeeded){
	if(parent._sysUiList){
		parent._sysUiList.refresh(succeeded);
		return;
	}
	if(parent.uiRefresh){
		parent.uiRefresh(succeeded);
		return;
	}
	if(!sysui){
		sysui = getSystemUiLayout();
	}
	if(sysui){
		sysui.closeForm(succeeded);
	}
};
//刷新数据
//参数type：all|table|left，默认table
UiUtil.refreshData=function(type){
	if(!sysui){
		sysui = getSystemUiLayout();
	}
	if(sysui){
		sysui.refreshData(type);
	}
};

//获取全局的上下文路径,在引用此脚本的页面中可以使用
var this_scriptSrc = document.getElementsByTagName('script')[document.getElementsByTagName('script').length - 1].src;
//当前脚本目录向上翻三层,找到上下文根目录 ,不含最后的/
UiUtil.path = this_scriptSrc.substring(0,this_scriptSrc.lastIndexOf('/'));
UiUtil.path = UiUtil.path.substring(0,UiUtil.path.lastIndexOf('/'));
UiUtil.path = UiUtil.path.substring(0,UiUtil.path.lastIndexOf('/'));

/**
 * 判断指定值是否包含异常或错误信息,并进行相应处理
 */
UiUtil.hasErrors = function(data, errCallback){
	if(!data){
		return false;
	}
	if(typeof data == 'string'){
		try {
			data = $.parseJSON(data);
		} catch (e) {
		}
	}
	if(data.exception){//服务器发生异常，输出异常错误信息
		var content = '服务器发生错误,请联系管理员:<br/>' + data.exception.exceptionClassName;
		var emsg = data.exception.message;
		if(emsg){
			content += ("<br/>"+emsg);
		}
		layer.msg(content, {icon: 2, end: function(){
			if(typeof errCallback == 'function'){
				errCallback.call(null, "exception");
			}
		}});
		/*jError(content,{
			VerticalPosition : 'top',
			HorizontalPosition : 'center',
			autoHide : true,
			clickOverlay : true,
			TimeShown : 2500,
			onClosed: function(){
				if(typeof errCallback == 'function'){
					errCallback.call(null, "exception");
				}
			}
	    });*/
		return true;
	}
	if(Object.prototype.toString.apply(data) === '[object Array]'){//数组
		if(data.length > 0 && data[0].sysError){//错误信息列表
			var errors = '发生错误!', field;
			$.each(data, function(index, d){
	    		errors += '<br/>' + (index + 1) + ':' + d.message;
	    		if(d.code){
	    			errors += '[错误码:' + d.code + ']';
	    		}
	    		if(d.field && !field){
	    			field = d.field;
	    		}
	        });
			
			layer.msg(errors, {icon: 2, end: function(){
				if(!!field){
					var errField = $('#'+field);
					if(!errField.size()){
						errField = $("[name='"+field+"']");
					}
					if(errField.size() && errField.is(":text")){
						errField.focus();
					}
				}
				if(typeof errCallback == 'function'){
					errCallback.call(null, "sys-error-list");
				}
			}});
			
			/*jError(errors,{
				VerticalPosition : 'top',
				HorizontalPosition : 'center',
				autoHide : false,
				clickOverlay : true,
				TimeShown : 2500,
				onClosed:function(){
					if(!!field){
						var errField = $('#'+field);
						if(!errField.size()){
							errField = $("[name='"+field+"']");
						}
						if(errField.size() && errField.is(":text")){
							errField.focus();
						}
					}
					if(typeof errCallback == 'function'){
						errCallback.call(null, "sys-error-list");
					}
				}
		    });*/
			return true;
		}
	}
	if(data.sysError){ //单一错误信息
		var errors = '发生错误!<br/>' + data.message;
		if(data.code){
			errors += '[错误码:' + data.code + ']';
		}
		layer.msg(errors, {icon: 2, end: function(){
			var field = data.field;
			if(!!field){
				var errField = $('#'+field);
				if(!errField.size()){
					errField = $("[name='"+field+"']");
				}
				if(errField.size() && errField.is(":text")){
					errField.focus();
				}
			}
    		if(typeof errCallback == 'function'){
				errCallback.call(null, "sys-error");
			}
		}});
    	/*jError(errors, {
			VerticalPosition : 'top',
			HorizontalPosition : 'center',
			autoHide : false,
			clickOverlay : true,
			TimeShown : 2500,
			onClosed:function(){
				var field = data.field;
				if(!!field){
					var errField = $('#'+field);
					if(!errField.size()){
						errField = $("[name='"+field+"']");
					}
					if(errField.size() && errField.is(":text")){
						errField.focus();
					}
				}
	    		if(typeof errCallback == 'function'){
					errCallback.call(null, "sys-error");
				}
    		}
	    });*/
		return true;
	}
	return false;
};

/**
 * 根据回调值判断是否调用成功,并进行相应处理
 */
UiUtil.isSuccess = function(data, successMessage, successCallback, errorCallback, resetButton){
	if(errorCallback && typeof errorCallback != 'function'){
		resetButton = errorCallback;
		errorCallback = null;
	}
	if(!UiUtil.hasErrors(data)){
		layer.msg(successMessage || "操作成功", {icon: 1, end: function(){
			if(successCallback){
				successCallback.call();
			}
		}});
		/*jSuccess(successMessage || "操作成功", {
			VerticalPosition : 'top',
			HorizontalPosition : 'center',
			onClosed: function(){
				if(successCallback){
					successCallback.call();
				}
			}
		});*/
	}else{
		if(errorCallback){
			errorCallback.call();
		}
	}
	if(resetButton !== false){
		UiUtil.unlockButton(resetButton);
	}
};

UiUtil.lockButton = function(id){
	id = id || "submit1";
	if(typeof id == 'string'){
		$("#"+id).prop("disabled",true);
	}else{
		$(id).prop("disabled",true);
	}
};
UiUtil.unlockButton = function(id){
	id = id || "submit1";
	if(typeof id == 'string'){
		$("#"+id).prop("disabled",false);
	}else{
		$(id).prop("disabled",false);
	}
};

